<template>
  <div class="productSelection">
    <!-- 新增装饰件选择 -->
    <TopFilter ref="TopFilterss" :list="topFilterList" :cols="2" labelWidth="90" @filterChange="changeHandle" />

    <div style="margin-top: 15px;"></div>
    <FilterTable ref="table" :onRowSelectChange="selectChange" :fetchapi="fetchApi" :params="fetchParams" :datakey="'rows'" :columns="columns" :onColumnsChange="columns => (this.columns = columns)" />
    <!-- <FilterTable
      ref="table"
      selectionType="single"
      :dataSource="dataSource"
      :onRowSelectChange="selectChange"
      :datakey="'records'"
      :columns="columns"
      :onColumnsChange="columns => this.columns = columns"
    /> -->

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button :disabled="this.isChoice" type="primary" @click="saveDialog">保 存</el-button>
    </div>
  </div>
</template>

<script>
// 数据字典/按钮权限
import { dictionary } from '@/utils/dictMixin';
import { queryUnitCode } from '@/api/part/partInfo';
import { vehicleConfiguration } from '@/api/retail/salesmanage/confirmcar'; // 获取五级菜单
import { partInfoMainData } from '@/api/part/qualityManagement/maintenanceBoutique';
export default {
  name: 'productSelection',
  mixins: [dictionary],
  props: {
    modifyType: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      topFilterList: this.createTopFilters(), // 搜索数据
      columns: this.createTableColumns(), // table数据
      fetchApi: partInfoMainData,
      fetchParams: {},
      selectedRows: [], // 选中列表中的行
      isChoice: true, // 按钮是否可以选择
      unitData: {} // 单位数据
    };
  },
  created() {
    // this.init();
    this.getUnit();
  },
  methods: {
    init() {
      vehicleConfiguration(0, {
        isUnlimited: 10041002
      })
        .then(res => {
          this.topFilterList[2].itemList = res.data;
        })
        .catch(err => {
          // console.log(err)
        });
    },
    selectChange(rows) {
      // 选中时触发的函数
      this.selectedRows = rows;
      this.isChoice = this.selectedRows.length > 0 ? false : true;
    },
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '主组号',
          fieldName: 'mainGroupNo',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输项目代码', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '装饰件代码',
          fieldName: 'partNo',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '车型代码',
          fieldName: 'model',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '拼音码',
          fieldName: '  ',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '装饰件名称',
          fieldName: 'partName',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '成套件',
          fieldName: 'isKitPart',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        }
      ];
    },

    changeHandle(val) {
      this.fetchParams = { ...val };
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '装饰件代码',
          dataIndex: 'partNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '装饰件名称',
          dataIndex: 'partName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '主组号',
          dataIndex: 'mainGroupNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '拼音码',
          dataIndex: 'spellCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型代码',
          dataIndex: 'model',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '含税销售单价',
          dataIndex: 'costSalesPrice',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '保质期',
          dataIndex: 'shelfDays',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '计量单位',
          dataIndex: 'unitCode',
          type: 'SELECT',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          render: props => {
            return <span>{this.unitData[props.row.unitCode]}</span>;
          }
        },
        {
          title: '套件',
          dataIndex: 'isKitPart',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isKitPart, '1004')}</span>;
          }
        }
      ];
    },
    // 获取计量单位
    async getUnit() {
      const res = await queryUnitCode();
      let obj = {};
      res.data.map(item => {
        obj[item.UNIT_CODE] = item.UNIT_NAME;
      });
      this.unitData = obj;
      this.columns.find(item => item.dataIndex == 'unitCode').filterItems = res.data.map(item => ({ text: item.UNIT_NAME, value: item.UNIT_CODE }));
    },
    closeDialog(param) {
      // 取消
      this.$emit('close', false, param);
    },
    saveDialog() {
      // 保存
      // console.log(this.selectedRows)
      if (this.selectedRows.length > 0) {
        this.$emit('close2', false, this.selectedRows);
      }
    }
  }
};
</script>

<style scoped="scoped" lang="less"></style>
